import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../manger/network_utils.dart';
import '../../components/custom_dialog.dart';
import '../../models/adress_info_model.dart';
import '../../services/adress_manger.dart';

class PaymentListPage extends StatefulWidget {
  const PaymentListPage({super.key});

  @override
  _PaymentListPageState createState() => _PaymentListPageState();
}

class _PaymentListPageState extends State<PaymentListPage> {
  List<CardInfo> cardList = []; // 假设这是从数据源加载的卡片列表
  String idCard = ""; // 选中的卡号
  final manager = AdressInfoManager();

  @override
  void initState() {
    super.initState();
    // 加载数据
    loadData();
  }

  // 模拟加载数据
  void loadData() async{
    final success = await manager.loadCardList();
    if (success.success) {
      setState(() {
        cardList = success.cardInfo ?? [];
      });
    } 
  }

  // 弹出删除确认框
  void showDeleteDialog() {
   showDialog(
      context: context,
      builder: (context) => CustomDialog(
        title: AppLocalizations.of(context)!.deleteAddress1,
        description: AppLocalizations.of(context)!.deleteAddress2,
        leftButtonText: AppLocalizations.of(context)!.cancel,
        rightButtonText: AppLocalizations.of(context)!.confirm,
        onLeftPressed: () => Navigator.pop(context),
        onRightPressed: () {
          Navigator.pop(context);
          deleteCard(idCard);
        },
      ),
    );
  }

  void deleteCard(String idCard) async{
    final success = await manager.deletteCard(idCard);
    if (success.success) {
      loadData();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context)!.fastPayment,style: const TextStyle(fontSize: 17)),
      ),
      body: cardList.isEmpty
          ? Center(child: Text(AppLocalizations.of(context)!.noQuickPayment))
          : ListView.builder(
              itemCount: cardList.length,
              itemBuilder: (context, index) {
                var model = cardList[index];
                return PaymentCardItem(
                  model: model,
                  onDelete: () {
                    idCard = model.paymentConsentId;
                    showDeleteDialog();
                  },
                );
              },
            ),
    );
  }
}

class PaymentCardItem extends StatelessWidget {
  final CardInfo model;
  final VoidCallback onDelete;

  const PaymentCardItem({super.key, required this.model, required this.onDelete});

@override
Widget build(BuildContext context) {
  return Container(
    margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
    decoration: BoxDecoration(
      color: Colors.white, // 设置背景为白色
      borderRadius: BorderRadius.circular(13),
    ),
    child: ListTile(
      contentPadding: EdgeInsets.symmetric(vertical: 10, horizontal: 16),
      leading: Image.asset("assets/images/adresstype0.png", width: 40, height: 32), // 设置图标
      title: Text(model.cardNo),
      trailing: IconButton(
        icon: Image.asset("assets/images/deletebtn.png", width: 18, height: 18), // 使用自定义的删除按钮图片
        onPressed: onDelete, // 点击删除按钮
      ),
    ),
  );
}

}
